<template>
  <div>
    <div style="margin-bottom: 15px">
      <el-input v-model="params2.id" style="width: 200px" placeholder="请输入家庭成员的id"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 10px" @click="findSelfInfoByUserID"></el-button>
      <el-button type="warning" icon="el-icon-refresh-right" style="margin-left: 10px" @click="reset"></el-button>
      <!--      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-left: 10px" @click="addUser">新增</el-button>-->
    </div>
    <div style="margin-bottom: 15px">
      <el-input v-model="newFamilyMemberId" style="width: 200px" placeholder="请输入想要邀请的家庭成员的id"></el-input>
      <el-button type="success" icon="el-icon-circle-plus-outline" style="margin-left: 10px" @click="inviteFamilyMember"></el-button>
      <!--      <el-button type="primary" icon="el-icon-circle-plus-outline" style="margin-left: 10px" @click="addUser">新增</el-button>-->
    </div>
    <!--用户信息表-->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="nickname" label="昵称"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!--            <el-button type="primary" icon="el-icon-edit" @click="editUser(scope.row)"></el-button>-->
            <el-popconfirm title="确定删除吗？" @confirm="() => deleteFamilyMember(user.id,scope.row.id)">
              <el-button slot="reference" type="danger" icon="el-icon-delete" style="margin-left: 5px"></el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--    &lt;!&ndash;分页查询&ndash;&gt;-->
    <!--    <div style="margin-top: 10px">-->
    <!--      <el-pagination-->
    <!--          @size-change="handleSizeChange"-->
    <!--          @current-change="handleCurrentChange"-->
    <!--          :current-page="params.pageNum"-->
    <!--          :page-sizes="[5, 10, 15, 20]"-->
    <!--          :page-size="params.pageSize"-->
    <!--          layout="total, sizes, prev, pager, next"-->
    <!--          :total="total">-->
    <!--      </el-pagination>-->
  </div>
  <!--新增&编辑用户表单-->
  <!--    <div>-->
  <!--      <el-dialog title="请填写用户信息" :visible.sync="dialogFormVisible" width="40%">-->
  <!--        <el-form :model="form">-->
  <!--          <el-form-item label="用户名">-->
  <!--            <el-input v-model="form.username" autocomplete="off" style="width: 80%"></el-input>-->
  <!--          </el-form-item>-->
  <!--        </el-form>-->
  <!--        <div slot="footer" class="dialog-footer">-->
  <!--          <el-button type="primary" @click="submitUserInfo">确 定</el-button>-->
  <!--          <el-button @click="dialogFormVisible = false">取 消</el-button>-->
  <!--        </div>-->
  <!--      </el-dialog>-->
  <!--    </div>-->
  <!--  </div>-->
</template>

<script>


import request from "@/utils/request";

export default {
  data() {
    return {
      params: {
        username: '',
        pageNum: 1,
        pageSize: 5
      },
      params2: {
        id: '',
        username: '',
        pageNum: 1,
        pageSize: 5
      },
      tableData: [],
      total: 0,
      dialogFormVisible: false,
      form: {},
      user: sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {},
      newFamilyMemberId: undefined
    }
  },
  created() {
    // this.load();
    this.findFamilyMemberByUserID();
  },
  methods: {
    //按id查询信息
    findSelfInfoByUserID() {
      request.get("/user/search/familyMember/" + this.user.id + "/" + this.params2.id).then(res => {
        if (res.code === '0') {
          console.log(res);
          this.tableData = res.data;
          this.total = 1;
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    // 邀请新家庭成员
    inviteFamilyMember(){
      request.post("/invitation/invite/"+this.user.id+"/"+this.newFamilyMemberId).then(res=>{
        if(res.code==='0'){
          this.$message({
            message: "邀请成功",
            type: 'success'
          })
        }else{
          this.$message({
            message: "邀请失败",
            type: 'error'
          })
        }
      })
    },
    //按id查询家庭信息
    findFamilyMemberByUserID() {
      request.get("/user/search/familyMember/" + this.user.id).then(res => {
        if (res.code === '0') {
          this.tableData = res.data;
          this.total = res.data.total;
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    //按用户名查询

    //清空
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 5,
        username: ''
      }
      this.findFamilyMemberByUserID()
    },
    //改变每页显示条数

    addUser() {
      this.dialogFormVisible = true;
      this.form = {};
    },
    editUser(obj) {
      this.form = obj;
      this.dialogFormVisible = true;
    },
    deleteFamilyMember(user_id, id) {
      request.delete("/user/delete/familyMember/" + user_id + "/" + id).then(res => {
        // console.log("09090909" + "我的id：" + user_id + "家庭成员id" + id)
        if (res.code === "0") {
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findFamilyMemberByUserID();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    submitFamilyMemberInfo() {
      request.post("/user/save", this.form).then(res => {
        if (res.code === "0") {
          this.$message({
            message: '操作成功',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.findFamilyMemberByUserID();
        } else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })

    }
  }
}
</script>
